package com.afkt.project.slice;

import com.afkt.project.model.item.ButtonList;
import com.afkt.project.model.item.ButtonValue;
import com.afkt.project.ui.adapter.ButtonAdapter;
import com.afkt.project.ui.slice.*;
import com.afkt.project.util.SkipUtils;
import com.ohos.afkt.ResourceTable;
import com.dev.DevUtils;
import com.dev.utils.app.toast.ToastTintUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.bundle.IBundleManager;

public class DevWidgetUISlice extends AbilitySlice {


    private DirectionalLayout bar_layout;

    private Image ui_return;

    private DirectionalLayout ui_bar;

    private ListContainer UI_list;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        //requestPermissions();
    }


    //申请权限
    private void requestPermissions() {
        if (verifySelfPermission("ohos.permission.CAMERA") != IBundleManager.PERMISSION_GRANTED) {
            // 应用未被授予权限
            if (canRequestPermission("ohos.permission.CAMERA")) {
                // 是否可以申请弹框授权(首次申请或者用户未选择禁止且不再提示)
                requestPermissionsFromUser(
                        new String[]{"ohos.permission.CAMERA"}, 0);
            } else {
                // 显示应用需要权限的理由，提示用户进入设置授权
                ToastTintUtils.success("该应用一定需要相机权限");
          /*      Intent intent = new Intent();
                Intent.OperationBuilder builder = new Intent.OperationBuilder();
                builder.withAction(IntentConstants.ACTION_MANAGE_APPLICATIONS_SETTINGS);
                intent.setOperation(builder.build());
                intent.addFlags(Intent.FLAG_ABILITY_CONTINUATION);
                startAbilityForResult(intent, 1);*/
            }
        } else {
            // 权限已被授予
        }
    }


    @Override
    public void onActive() {
        super.onActive();


        bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_bar_layout);
        ui_bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_ui_bar, null, false);
        Text text = (Text) ui_bar.findComponentById(ResourceTable.Id_title_name);
        text.setText("DevWidget UI 库");
        bar_layout.addComponent(ui_bar);
        ui_return = (Image) ui_bar.findComponentById(ResourceTable.Id_ui_return);
        ui_return.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });
        UI_list = (ListContainer) findComponentById(ResourceTable.Id_UI_List);
        ButtonAdapter buttonAdapter = new ButtonAdapter(ButtonList.getModuleDevWidgetButtonValues(), this);
        UI_list.setItemProvider(buttonAdapter);
        UI_list.setItemClickedListener((listContainer, component, i, l) -> {
            ButtonValue buttonValue = (ButtonValue) UI_list.getItemProvider().getItem(i);
            switch (buttonValue.type) {
                case ButtonValue.BTN_VIEW_PAGER: // ViewPager 滑动监听、控制滑动
                    SkipUtils.startActivity(new ViewPagerActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_CUSTOM_PROGRESS_BAR: // 自定义 ProgressBar 样式 View
                    SkipUtils.startActivity(new ProgressBarActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_SCAN_VIEW: // 自定义扫描 View ( QRCode、AR )
//                        SkipUtils.startActivity(new ScanShapeActivity(), buttonValue);
                    SkipUtils.startNewAbility(DevWidgetUISlice.this,"com.ohos.afkt","com.afkt.project.NewTaskQRAbility");
                    break;
                case ButtonValue.BTN_WRAP_VIEW: // 自动换行 View
                    SkipUtils.startActivity(new WrapSlice(), buttonValue);
                    break;
                case ButtonValue.BTN_SIGN_VIEW: // 签名 View
                    SkipUtils.startActivity(new SignActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_LINE_VIEW: // 换行监听 View
                    SkipUtils.startActivity(new WrapSelectSlice(), buttonValue);
                    break;
                case ButtonValue.BTN_LIKE_VIEW: // 自定义点赞效果 View
                    SkipUtils.startActivity(new FlowLikeSlice(), buttonValue);
                    break;
                case ButtonValue.BTN_CORNER_LABEL_VIEW: // 自定义角标 View
                    SkipUtils.startActivity(new CornerLabelActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_VIEW_ASSIST: // View 填充辅助类
                    SkipUtils.startActivity(new ViewAssitSlice(), buttonValue);
                    break;
            }
        });
    }

    @Override
    protected void onBackground() {
        bar_layout.removeComponent(ui_bar);
        super.onBackground();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
